var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
    data: {
        markers: [],
        rules: [],
        mapCtx: '',
        formData: {}, //表单的内容
        latitude: '',
        longitude: '',
        carId: ''

    },
    onLoad: function(event) {
        console.log(event.carId)
        this.data.carId = event.carId

        const that = this
            // 实例化API核心类
        qqmapsdk = new QQMapWX({
            key: 'NOVBZ-ECFCS-GC6OG-6ZMSN-2RPGJ-52FYI'
        });
        that.mapCtx = wx.createMapContext('map')
        wx.getLocation({ //微信获取当前位置
            type: 'gcj02',
            isHighAccuracy: 'true',
            success(res) {
                console.log(res)
                var marker = [{
                    latitude: res.latitude,
                    longitude: res.longitude,
                    iconPath: '../../images/map/curloc.png',
                    width: 24,
                    height: 24
                }]
                that.setData({
                    latitude: res.latitude,
                    longitude: res.longitude,
                    markers: marker
                })
            }
        })
    },


    bindFormSubmit(e) {
        console.log(e)
        const params = {
            carid: this.data.carId,
            latitude: this.data.latitude,
            floor: this.data.formData.floor || "一层",
            longitude: this.data.longitude,
            note: e.detail.value.textarea || "",
            status: 'stop',
            time: new Date().toTimeString().split(' ')[0],
        }
        console.log(params)

        wx.cloud.callFunction({ // 调取云函数 获取用户车辆信息
            name: 'carstop',
            data: params,
            success: res => {
                console.log('本地', res)
                if (res.result == 'success') {
                    wx.showToast({
                        title: '成功停车',
                        icon: 'success',
                    });
                    wx.switchTab({
                        url: '../car/car' //页面跳转
                    })
                }

            }
        })

    },
    formInputChange(e) { // 表单修改
        const { field } = e.currentTarget.dataset
        this.setData({
            [`formData.${field}`]: e.detail.value
        })
    },
})